<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SessionStorage高级调试工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .section {
            margin-bottom: 30px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .section h3 {
            margin-top: 0;
            color: #333;
        }
        .data-item {
            margin: 10px 0;
            padding: 10px;
            background: #f9f9f9;
            border-radius: 3px;
            border-left: 4px solid #007bff;
        }
        .key {
            font-weight: bold;
            color: #0066cc;
            margin-bottom: 5px;
        }
        .value {
            white-space: pre-wrap;
            font-family: monospace;
            background: #f0f0f0;
            padding: 5px;
            border-radius: 3px;
            max-height: 200px;
            overflow-y: auto;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .clear-btn {
            background: #dc3545;
        }
        .clear-btn:hover {
            background: #c82333;
        }
        .test-btn {
            background: #28a745;
        }
        .test-btn:hover {
            background: #218838;
        }
        .status {
            padding: 10px;
            border-radius: 5px;
            margin: 10px 0;
        }
        .status.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .status.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .status.warning {
            background: #fff3cd;
            color: #856404;
            border: 1px solid #ffeaa7;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>SessionStorage高级调试工具</h1>
        
        <div class="section">
            <h3>操作按钮</h3>
            <button onclick="refreshData()">刷新数据</button>
            <button onclick="clearAllData()" class="clear-btn">清空所有数据</button>
            <button onclick="exportData()">导出数据</button>
            <button onclick="testDataPersistence()" class="test-btn">测试数据持久化</button>
        </div>
        
        <div class="section">
            <h3>数据持久化测试</h3>
            <div id="persistenceTest"></div>
        </div>
        
        <div class="section">
            <h3>退住申请数据统计</h3>
            <div id="checkoutStats"></div>
        </div>
        
        <div class="section">
            <h3>所有SessionStorage数据</h3>
            <div id="allData"></div>
        </div>
        
        <div class="section">
            <h3>退住申请相关数据</h3>
            <div id="checkoutData"></div>
        </div>
        
        <div class="section">
            <h3>特定业务ID数据查询</h3>
            <input type="text" id="businessIdInput" placeholder="输入业务ID" style="padding: 5px; margin-right: 10px;">
            <button onclick="getSpecificData()">获取特定数据</button>
            <div id="specificData"></div>
        </div>
    </div>

    <script>
        function refreshData() {
            displayAllData();
            displayCheckoutData();
            displayCheckoutStats();
        }
        
        function displayAllData() {
            const container = document.getElementById('allData');
            container.innerHTML = '';
            
            if (sessionStorage.length === 0) {
                container.innerHTML = '<p>没有数据</p>';
                return;
            }
            
            for (let i = 0; i < sessionStorage.length; i++) {
                const key = sessionStorage.key(i);
                const value = sessionStorage.getItem(key);
                
                const dataItem = document.createElement('div');
                dataItem.className = 'data-item';
                dataItem.innerHTML = `
                    <div class="key">${key}</div>
                    <div class="value">${value}</div>
                `;
                container.appendChild(dataItem);
            }
        }
        
        function displayCheckoutData() {
            const container = document.getElementById('checkoutData');
            container.innerHTML = '';
            
            // 查找所有退住申请相关的数据
            const checkoutKeys = [];
            for (let i = 0; i < sessionStorage.length; i++) {
                const key = sessionStorage.key(i);
                if (key.includes('checkout_approval_data')) {
                    checkoutKeys.push(key);
                }
            }
            
            if (checkoutKeys.length === 0) {
                container.innerHTML = '<p>没有退住申请数据</p>';
                return;
            }
            
            checkoutKeys.forEach(key => {
                const value = sessionStorage.getItem(key);
                let parsedValue;
                try {
                    parsedValue = JSON.parse(value);
                } catch (e) {
                    parsedValue = value;
                }
                
                const dataItem = document.createElement('div');
                dataItem.className = 'data-item';
                dataItem.innerHTML = `
                    <div class="key">${key}</div>
                    <div class="value">${JSON.stringify(parsedValue, null, 2)}</div>
                `;
                container.appendChild(dataItem);
            });
        }
        
        function displayCheckoutStats() {
            const container = document.getElementById('checkoutStats');
            container.innerHTML = '';
            
            // 统计退住申请数据
            const checkoutKeys = [];
            for (let i = 0; i < sessionStorage.length; i++) {
                const key = sessionStorage.key(i);
                if (key.includes('checkout_approval_data')) {
                    checkoutKeys.push(key);
                }
            }
            
            let statsHtml = `<p>退住申请数据统计：</p>`;
            statsHtml += `<p>总数量：${checkoutKeys.length}</p>`;
            
            if (checkoutKeys.length > 0) {
                statsHtml += `<p>存储键列表：</p>`;
                checkoutKeys.forEach(key => {
                    const value = sessionStorage.getItem(key);
                    try {
                        const data = JSON.parse(value);
                        statsHtml += `<p>• ${key} - ${data.elderlyName || '未知'}</p>`;
                    } catch (e) {
                        statsHtml += `<p>• ${key} - 数据解析失败</p>`;
                    }
                });
            }
            
            container.innerHTML = statsHtml;
        }
        
        function testDataPersistence() {
            const container = document.getElementById('persistenceTest');
            container.innerHTML = '';
            
            // 模拟测试数据持久化
            const testData1 = {
                elderlyName: '赵老十四',
                elderlyIdCard: '310104193409090077',
                checkoutReason: '老人个人原因',
                businessId: 59
            };
            
            const testData2 = {
                elderlyName: '徐老六',
                elderlyIdCard: '330102195712160012',
                checkoutReason: '费用高昂',
                businessId: 60
            };
            
            // 保存测试数据
            const storageKey1 = `checkout_approval_data_${testData1.businessId}`;
            const storageKey2 = `checkout_approval_data_${testData2.businessId}`;
            
            sessionStorage.setItem(storageKey1, JSON.stringify(testData1));
            sessionStorage.setItem(storageKey2, JSON.stringify(testData2));
            
            // 测试数据获取
            const retrievedData1 = sessionStorage.getItem(storageKey1);
            const retrievedData2 = sessionStorage.getItem(storageKey2);
            
            let testHtml = '<h4>数据持久化测试结果：</h4>';
            
            if (retrievedData1) {
                const data1 = JSON.parse(retrievedData1);
                testHtml += `<div class="status success">✓ 赵老十四数据正确保存和获取</div>`;
                testHtml += `<p>存储键：${storageKey1}</p>`;
                testHtml += `<p>数据：${data1.elderlyName} - ${data1.checkoutReason}</p>`;
            } else {
                testHtml += `<div class="status error">✗ 赵老十四数据获取失败</div>`;
            }
            
            if (retrievedData2) {
                const data2 = JSON.parse(retrievedData2);
                testHtml += `<div class="status success">✓ 徐老六数据正确保存和获取</div>`;
                testHtml += `<p>存储键：${storageKey2}</p>`;
                testHtml += `<p>数据：${data2.elderlyName} - ${data2.checkoutReason}</p>`;
            } else {
                testHtml += `<div class="status error">✗ 徐老六数据获取失败</div>`;
            }
            
            // 测试数据隔离
            const crossData1 = sessionStorage.getItem(storageKey2);
            const crossData2 = sessionStorage.getItem(storageKey1);
            
            if (crossData1 && crossData2) {
                const data1 = JSON.parse(crossData1);
                const data2 = JSON.parse(crossData2);
                
                if (data1.elderlyName !== data2.elderlyName) {
                    testHtml += `<div class="status success">✓ 数据隔离正确，不同申请显示不同数据</div>`;
                } else {
                    testHtml += `<div class="status error">✗ 数据隔离失败，不同申请显示相同数据</div>`;
                }
            }
            
            container.innerHTML = testHtml;
            refreshData();
        }
        
        function getSpecificData() {
            const businessId = document.getElementById('businessIdInput').value;
            if (!businessId) {
                alert('请输入业务ID');
                return;
            }
            
            const storageKey = `checkout_approval_data_${businessId}`;
            const value = sessionStorage.getItem(storageKey);
            
            const container = document.getElementById('specificData');
            if (value) {
                let parsedValue;
                try {
                    parsedValue = JSON.parse(value);
                } catch (e) {
                    parsedValue = value;
                }
                
                container.innerHTML = `
                    <div class="data-item">
                        <div class="key">${storageKey}</div>
                        <div class="value">${JSON.stringify(parsedValue, null, 2)}</div>
                    </div>
                `;
            } else {
                container.innerHTML = '<p>没有找到对应的数据</p>';
            }
        }
        
        function clearAllData() {
            if (confirm('确定要清空所有SessionStorage数据吗？')) {
                sessionStorage.clear();
                refreshData();
                alert('数据已清空');
            }
        }
        
        function exportData() {
            const data = {};
            for (let i = 0; i < sessionStorage.length; i++) {
                const key = sessionStorage.key(i);
                const value = sessionStorage.getItem(key);
                try {
                    data[key] = JSON.parse(value);
                } catch (e) {
                    data[key] = value;
                }
            }
            
            const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'sessionStorage_data.json';
            a.click();
            URL.revokeObjectURL(url);
        }
        
        // 页面加载时显示数据
        window.onload = function() {
            refreshData();
        };
    </script>
</body>
</html>
